$(function() {
    //给节点等级添加子项
    var rootLeafHtml = "";
    var rootLeafIndex = 2; //节点层级
    for(let i = 0; i < rootLeafIndex; i++) {
        rootLeafHtml += "<option value='" + i + "'>" + i + "</option>";
    }
    $("#rootLeaf").html(rootLeafHtml);
    $("#rootLeaf").val(0);

    //给排序等级添加子项(默认5个排序等级)
    var sortLevel = 5;
    var sortLeverHtml = "";
    for(let i = 0; i < sortLevel; i++) {
        sortLeverHtml += "<option value='" + i + "'>" + i + "</option>"
    }
    $("#sortLevel").html(sortLeverHtml);

    //提交表单
    $("#submit").click(function() {
        var remark = $("#remark").val();
        var pathName = $("#pathName").val();
        var rootlevel = $("#rootLeaf").val();
        var fatherLeaf = $("#fatherLeaf").val();
        if(fatherLeaf == null){
            fatherLeaf =0;
        }
        var sonLeaf = $('#sonLeaf').val();
        var sortLevel = $("#sortLevel").val();
        console.log("0::" + remark + "::1::" + pathName + "::2::" + rootlevel + "::3::" + fatherLeaf + "::4::" + sonLeaf + "::5::" + sortLevel);
        $.post("http://localhost:8081/study/path/addPath", {
            remark: remark,
            pathName: pathName,
            rootLeaf: rootlevel,
            fatherLeaf: fatherLeaf,
            sonLeaf: sonLeaf,
            sortLevel: sortLevel
        }, function(data) {
            console.log(data)
            if(data.success){
                alert(data.message);
                window.location.href="addPath.html"
            }else{
                alert(data.message)
            }
        })
    })



    //禁用form表单自定义action提交功能
    $(document).ready(function() {
        $("form").submit(function(e) {
            e.preventDefault();
            console.log("Submit prevented");
        });
    });
})

function chooseFatherLeaf() {
    //异步加载自动选着父节点
    $("#fatherLeaf").html("");
    var fatherLeaf = parseInt($("#rootLeaf").val()) - 1;
    if(fatherLeaf >= 0) {
        $.post("http://localhost:8081/study/path/findFatherLeaf", {
            fatherLeaf: fatherLeaf
        }, function(data) {
            if(data.success) {
                console.log(data.message);
                console.log(data);
                var fatherLeafList = data.list;
                var fatherLeafInnerHtml = "";
                //循环遍历返回回来的json数据
                $.each(fatherLeafList, function(key, data) {
                    console.log(data.id + "::" + data.remark);
                    fatherLeafInnerHtml +=
                        "<option value='" + data.id + "'>" + data.remark + "</option>"
                });
                console.log(fatherLeafInnerHtml);
                $("#fatherLeaf").html(fatherLeafInnerHtml);
            } else {
                console.log(data.message)
            }
        })
    }
}